package com.zone.core.ext

import java.text.SimpleDateFormat
import java.util.*

/**
 *Author: last
 *Time: 2020-01-16
 *Description: 对Data的扩展
 */
fun Date.getFormat(format: String): String {
    val dateFormat = SimpleDateFormat(format, Locale.getDefault())
    return dateFormat.format(this)
}

/**
 * 星期几
 * @return 一、二、三。。。
 */
fun Date.weekDay(): String {
    val calendar = Calendar.getInstance()
    calendar.time = this
    calendar.get(Calendar.DAY_OF_WEEK)
    return when (calendar.get(Calendar.DAY_OF_WEEK)) {
        1 -> "日"
        2 -> "一"
        3 -> "二"
        4 -> "三"
        5 -> "四"
        6 -> "五"
        7 -> "六"
        else -> ""
    }
}

/**
 * 星期几
 * @return 一、二、三。。。
 */
fun Date.weekDayIndex(): String {
    val calendar = Calendar.getInstance()
    calendar.time = this
    calendar.get(Calendar.DAY_OF_WEEK)
    return when (calendar.get(Calendar.DAY_OF_WEEK)) {
        1 -> "0"
        2 -> "1"
        3 -> "2"
        4 -> "3"
        5 -> "4"
        6 -> "5"
        7 -> "6"
        else -> "-"
    }
}

/**
 * 周几
 * @return 周一、周二、周三。。。
 */
fun Date.weekDayStr(): String {
    val weekDay = weekDay()
    return "周$weekDay"
}


const val FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss"
const val FORMAT_DATE = "yyyy-MM-dd"
const val FORMAT_TIME = "HH:mm:ss"

/**
 * 时间格式转换为DateFormat
 */
fun String.toDateFormat(): SimpleDateFormat {
    return SimpleDateFormat(this, Locale.getDefault())
}


/**
 * 日期格式转换
 * @param currentFormat 当前格式
 * @param targetFormat 目标格式
 */
fun String?.convertFormat(currentFormat: String? = FORMAT_DEFAULT, targetFormat: String?): String {
    if (this == null) {
        return ""
    }
    return try {
        currentFormat?.toDateFormat()?.parse(this)?.let {
            targetFormat?.toDateFormat()?.format(it)
        } ?: ""
    } catch (e: Exception) {
        e.printStackTrace()
        ""
    }
}

/**
 * 日期格式化
 * @param targetFormat 目标格式
 */
fun Date.format(targetFormat: String? = FORMAT_DEFAULT): String {
    try {
        return targetFormat?.toDateFormat()?.format(this) ?: ""
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return ""
}

/**
 * 字符串转换为date
 * @receiver String? 字符串
 * @param format String? 格式
 * @return Date? date
 */
fun String?.toDate(format: String? = FORMAT_DEFAULT): Date? {
    if (this == null) {
        return null
    }
    return try {
        format?.toDateFormat()?.parse(this)
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}

/**
 * 字符串转换为时间戳
 * @receiver String? 时间字符串
 * @param format String? 格式
 * @return Long? 时间戳
 */
fun String?.toTime(format: String? = FORMAT_DEFAULT): Long? {
    if (this == null) {
        return null
    }
    return try {
        format?.toDateFormat()?.parse(this)?.time
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}


/**
 * 两个日期是否同一天
 * @receiver Date?
 * @param date Date
 * @return Boolean
 */
fun Date?.isSameDay(date: Date): Boolean {
    if (this == null) {
        return false
    }
    return toCalendar().isSameDay(date.toCalendar())
}

/**
 * 是否当天
 * @receiver Date?
 * @return Boolean
 */
fun Date?.isToday(): Boolean {
    if (this == null) {
        return false
    }
    return isSameDay(Date())
}

/**
 * Date转换Calendar
 * @receiver Date
 * @return Calendar
 */
fun Date.toCalendar(): Calendar {
    val calendar = Calendar.getInstance()
    calendar.time = this
    return calendar
}

/**
 * 是否同一天
 * @receiver Calendar?
 * @param calendar Calendar
 * @return Boolean
 */
fun Calendar?.isSameDay(calendar: Calendar): Boolean {
    if (this == null) {
        return false
    }

    return get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
            && get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
            && get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)
}


fun Calendar?.diffDays(calendar: Calendar?): Int {
    if(this == null || calendar == null){
        return 0
    }

    val cal1 = if(this < calendar)  this else calendar
    val cal2 = if(this < calendar)  calendar else this


    //获取日期在一年(月、星期)中的第多少天
    //获取日期在一年(月、星期)中的第多少天
    val day1: Int = cal1.get(Calendar.DAY_OF_YEAR)

    val day2: Int = cal2.get(Calendar.DAY_OF_YEAR)


    //获取当前日期所在的年份
    //获取当前日期所在的年份
    val year1: Int = cal1.get(Calendar.YEAR)
    val year2: Int = cal2.get(Calendar.YEAR)

    //如果两个日期的是在同一年，则只需要计算两个日期在一年的天数差；
    //不在同一年，还要加上相差年数对应的天数，闰年有366天

    //如果两个日期的是在同一年，则只需要计算两个日期在一年的天数差；
    //不在同一年，还要加上相差年数对应的天数，闰年有366天
    return if (year1 != year2) { //不同年
        var timeDistance = 0
        for (i in year1 until year2) {
            timeDistance += if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
                366
            } else {//不是闰年
                365
            }
        }
        timeDistance + (day2 - day1)
    } else {//同年
        day2 - day1
    }
}

fun Date?.diffDays(date: Date?): Int {
    if(this == null || date == null){
        return 0
    }
    return this.toCalendar().diffDays(date.toCalendar())
}